﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public sealed  class ESF_SingleSingleManager
{
    static Dictionary<int, List<IESF_Single>> m_DicAllSingle = new Dictionary<int, List<IESF_Single>>();
    private static readonly List<IESF_Single> m_DefaultValue = new List<IESF_Single>();
    /// <summary>
    /// 获取所有的Manager
    /// </summary>
    /// <returns></returns>
    public static List<IESF_Single> GetAllSingleManager() {
        return m_DefaultValue;
    }
    public static void AddSingle<T>(IESF_Single _single) {
        Type t = typeof(T);
        Type[] inter= t.GetInterfaces();
        for (int i = 0; i < inter.Length; i++)
        {
            Type intertamp = inter[i];
            int hashCode = intertamp.FullName.GetHashCode();
            List<IESF_Single> tampList;
            if (!m_DicAllSingle.TryGetValue(hashCode, out tampList)) {
                tampList = new List<IESF_Single>();
                m_DicAllSingle[hashCode] = tampList;
            }
            if (!tampList.Contains(_single)) {
                tampList.Add(_single);
            }
        }
    }
   
    public static List<T> GetSingles<T>() {
        Type t = typeof(T);
        
        int hashCode = t.FullName.GetHashCode();
        List<IESF_Single> tampList;
        if (!m_DicAllSingle.TryGetValue(hashCode, out tampList)) {
            tampList = m_DefaultValue;
        }
        List<T> returnList = new List<T>();
        for (int i = 0; i < tampList.Count; i++)
        {
            if (tampList[i] is T)
            {
                returnList.Add((T)tampList[i]);
            }

        }
        return returnList;
    }
    public static bool HasSingle<T>() {
        return GetSingles<T>().Count > 0;
    }
}
